<template>
  <div>
    <el-table :data="articles" style="width: 100%" size="mini">
      <el-table-column label="文章标题" prop="title"> </el-table-column>
      <el-table-column label="发布时间" prop="createTime"> </el-table-column>
      <el-table-column label="文章来源" prop="stemfrom"> </el-table-column>
      <el-table-column label="阅读量" prop="read"> </el-table-column>
      <el-table-column label="点赞数" prop="star"> </el-table-column>
      <el-table-column label="评论数" prop="comment"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div class="buttonbox">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页按钮 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      :page-size="pageSize"
      :current-page="page"
      @current-change="changePage"
    >
    </el-pagination>
  </div>
</template>
<script>
import { getAllArticles, delArticle } from "@/api/article";
export default {
  data() {
    return {
      articles: [],
      page: 1, //当前页数
      pageSize: 0, //每一页条数
      count: 0, //总条数
    };
  },
  created() {
    this.getDate();
  },
  methods: {
    async getDate() {
      // 获取用户文章列表数据
      const { data } = await getAllArticles({
        page: this.page,
        author: this.$store.state.user.username,
      })
      this.articles = data.result; 
      this.pageSize = data.pagesize;
      this.count = data.count;
      
    },
    handleEdit(row) {
       this.$router.push({
           path:'/admin/article/update',
           query:{
               id:row.id
           }
       })

    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delArticle({ id: row.id }).then((rel) => {
            if (rel.data.code == 200) {
              this.$message({
                type: "success",
                message: "文章删除成功!",
              });
              this.getDate();
            } else {
              this.$message({
                type: "error",
                message: "删除失败!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    changePage(page) {
      this.page = page;
      this.getDate();
    },
  },
};
</script>
<style lang="less" scoped>
.buttonbox {
  display: flex;
}
</style>